package com.ncucoder.demo.service.impl;

import com.ncucoder.demo.mapper.CustomerMapper;
import com.ncucoder.demo.model.Customer;
import com.ncucoder.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Implementation of customer service
 *
 * @author <a href="https://edit.ncucoder.com">hsowan</a>
 * @since 2018-12-24 22:41
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Customer getCustomerById(Long id) {
        if (id > 0){
            return customerMapper.getCustomerById(id);
        }
        return null;
    }

    @Override
    public List<Customer> listCustomers(int page, int size) {
        int min = size * (page - 1);
        int max = min + size;
        return customerMapper.listCustomers(min, max, min, size);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveCustomer(Customer customer) {
        if (customer != null){
            return customerMapper.saveCustomer(customer);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCustomerById(Long id) {
        if (id > 0){
            return customerMapper.deleteCustomerById(id);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCustomer(Customer customer) {
        if (customer != null){
            return customerMapper.updateCustomer(customer);
        }
        return 0;
    }

    @Override
    public int count() {
        return customerMapper.count();
    }


}
